package com.tqjc.execute.utils;

import com.tqjc.area.entity.SteelPlate;
import com.tqjc.system.core.util.DateUtils;
import com.tqjc.system.core.util.UUIDUtil;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @Author: 86195
 * @CreateTime: 2024-05-25  14:53
 * @Description: 文件导入输出流
 * @Version: 1.0
 */
public class ExcelUtils {

    public static void buildData(Workbook wb, List<SteelPlate> list) {
        // 创建sheet页
        Sheet sheetName = wb.createSheet("sheetName");

        // 填充表头
        Row row = sheetName.createRow(0);
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("钢板号");
        row.createCell(2).setCellValue("垛位");
        row.createCell(3).setCellValue("钢板长度");
        row.createCell(4).setCellValue("钢板厚度");
        row.createCell(5).setCellValue("钢板宽度");
        row.createCell(6).setCellValue("钢板重量");
        row.createCell(7).setCellValue("创建时间");

        // 填充内容
        for (int i = 0; i < list.size(); i++) {
            SteelPlate steelPlate = list.get(i);
            row = sheetName.createRow(i + 1);
            row.createCell(0).setCellValue(i+1);
            if(StringUtils.isNotEmpty(steelPlate.getSteelId())){
                row.createCell(1).setCellValue(steelPlate.getSteelId());
            }
            if(StringUtils.isNotEmpty(steelPlate.getArea())){
                row.createCell(2).setCellValue(getStockName(steelPlate.getArea()));
            }
            if(steelPlate.getLength() != null){
                row.createCell(3).setCellValue(steelPlate.getLength());
            }
            if(steelPlate.getThickness() != null){
                row.createCell(4).setCellValue(steelPlate.getThickness());
            }
            if(steelPlate.getWidth() != null){
                row.createCell(5).setCellValue(steelPlate.getWidth());
            }
            if(steelPlate.getWeight() != null){
                row.createCell(6).setCellValue(steelPlate.getWeight());
            }
            if(steelPlate.getCreateDate() != null){
                row.createCell(7).setCellValue(DateUtils.formatDateByPattern(steelPlate.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            }
        }

        // sheet页重命名
        wb.setSheetName(0, "sheet");
    }




    public static void buildStyle(Workbook wb) {
        Sheet sheet;
        Row row;
        for (int s = 0; s < wb.getNumberOfSheets(); s++) { // 遍历Sheet页
            sheet = wb.getSheetAt(s);
            for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++) { // 遍历所有行
                row = sheet.getRow(r);
                for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { // 遍历每一样所有单元格
                    if (r == 0) { // 表头
                        sheet.autoSizeColumn(c); // 列自动伸缩
                    } else { // 数据
                        if (sheet.getColumnWidth(c) > 50 * 256) {
                            sheet.setColumnWidth(c, 50 * 256); // 设置列宽
                        }
                    }
                }
            }
        }
    }


    //FileOutputStream
    public static String getFileName() throws Exception {
        LocalDateTime time = LocalDateTime.now(); // get the current time
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = time.format(formatter);
        String fileName= dateStr + "_" + UUIDUtil.getUUID() + ".xlsx";

        return fileName;
    }




    private static String getStockName(String area){

        if(StringUtils.isEmpty(area)){
            return "未知垛位";
        }

        switch (area){
            case "stock_1":
                return "2#横移垛位";
            case "stock_2":
                return "3#横移垛位";
            case "stock_3":
                return "4#横移垛位";
            case "stock_4":
                return "5#横移垛位";
            case "stock_5":
                return "11C北_垛位1";
            case "stock_6":
                return "11C北_垛位2";
            case "stock_7":
                return "11C南_垛位1";
            case "stock_8":
                return "11C南_垛位2";
            default:
                return "未知垛位";
        }

    }
}
